#!/usr/bin/env python
# -*- coding: utf-8 -*-

########################################################################
# PSIMON - Python System Information MONitor                           #
#                                                                      #
# Requirements:                                                        #
# - pystatgrab 0.5 (http://www.i-scream.org/pystatgrab/)               #
#                                                                      #
# Copyright (C) 2009 Simon Lundmark <simon.lundmark@gmail.com>         #
##***###################################################################
# This file is part of PSIMON.                                         #
#                                                                      #
# PSIMON is free software: you can redistribute it and/or modify       #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or    #
# (at your option) any later version.                                  #
#                                                                      #
# PSIMON is distributed in the hope that it will be useful,            #
# but WITHOUT ANY WARRANTY; without even the implied warranty of       #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        #
# GNU General Public License for more details.                         #
#                                                                      #
# You should have received a copy of the GNU General Public License    #
# along with PSIMON.  If not, see <http://www.gnu.org/licenses/>.      #
########################################################################

import sys
import os
import statgrab
import curses
import time

refresh_rate = 1

#
# Percentage stuff
#

class Percentage:
	def __init__(self, actual, total):
		self.actual = actual * 1.0
		self.total = total * 1.0
	
	def quota(self):
		if self.total == 0:
			return 0.0;
		return (self.actual) / self.total
	
	def __str__(self):
		return str(int((self.quota()*100))) + " %"
	
	def bar(self, width):
		width = width - 2
		length = int(self.quota() * width)
		bar = "["
		for i in range(length):
			bar = bar + "="
		for i in range(width - length):
			bar = bar + "-"
		bar = bar + "]"
		return bar

#
# Curses stuff
#

# TODO A Table class

# Add a table to the curses window.
# win	The curses window
# y		y-position
# x		x-position
# w		A list of column widths
# data	The matrix containing the data
def addtable(win, y, x, w, data):
	for row in range(len(data)):
		for col in range(len(w)):
			win.addstr(y+row, x+col+sum(w[:col]), str(data[row][col]))


def jolt(stdscr):
	curses.curs_set(0)
	curses.use_default_colors()
	while 1:
		stdscr.erase()
		ymax, xmax = stdscr.getmaxyx()
		y = 0
		# Show time
		timestr = time.strftime("Time: %H:%M:%S")
		stdscr.addstr(y, xmax-len(timestr), timestr)
		y += 1
		# Show filesystem stats
		fs_stats = statgrab.sg_get_fs_stats()
		data = list()
		data.append(["Mount point:", "Size (GB):", "Used (GB):", "Free (GB):", ""])
		for mp in fs_stats:
			data.append([mp['mnt_point'], mp['size']/1000000000, mp['used']/1000000000, (mp['size']-mp['used'])/1000000000, Percentage(mp['used'], mp['size']).bar(25)])
		addtable(stdscr, y, 0, [14, 12, 12, 10, 25], data)
		y += len(fs_stats)
		# Test a table
		#data = list()
		#data.append(["a", "b", "c"])
		#data.append(["d", "e", "f"])
		#addtable(stdscr, y, 10, [5, 5, 5], data)
		stdscr.refresh()
		time.sleep(refresh_rate)


#
# Main
#

def main(*args):
	try:
		curses.wrapper(jolt)
	except KeyboardInterrupt:
		print "Interrupted by user"
		return 0
	except:
		print "Unexpected error:", sys.exc_info()[0]
		raise
	else:
		# exit errorlessly
		return 0

if __name__=="__main__":
	sys.exit(main(*sys.argv))

